using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public static class MetroInMotion
	{
		public static readonly DependencyProperty AnimationLevelProperty = DependencyProperty.RegisterAttached("AnimationLevel", typeof(int), typeof(MetroInMotion), new PropertyMetadata(-1));
		public static readonly DependencyProperty IsPivotAnimatedProperty = DependencyProperty.RegisterAttached("IsPivotAnimated", typeof(bool), typeof(MetroInMotion), new PropertyMetadata(false, new PropertyChangedCallback(MetroInMotion.OnIsPivotAnimatedChanged)));
		public static int GetAnimationLevel(DependencyObject dependencyObject_0)
		{
			return (int)dependencyObject_0.GetValue(MetroInMotion.AnimationLevelProperty);
		}
		public static void SetAnimationLevel(DependencyObject dependencyObject_0, int value)
		{
			dependencyObject_0.SetValue(MetroInMotion.AnimationLevelProperty, value);
		}
		public static bool GetIsPivotAnimated(DependencyObject dependencyObject_0)
		{
			return (bool)dependencyObject_0.GetValue(MetroInMotion.IsPivotAnimatedProperty);
		}
		public static void SetIsPivotAnimated(DependencyObject dependencyObject_0, bool value)
		{
			dependencyObject_0.SetValue(MetroInMotion.IsPivotAnimatedProperty, value);
		}
		private static void OnIsPivotAnimatedChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs args)
		{
			ItemsControl list = dependencyObject_0 as ItemsControl;
			list.add_Loaded(delegate(object sender, RoutedEventArgs e)
			{
				Pivot pivot = Enumerable.Single<DependencyObject>(list.Ancestors<Pivot>()) as Pivot;
				int pivotIndex = pivot.get_Items().IndexOf(Enumerable.Single<DependencyObject>(list.Ancestors<PivotItem>()));
				bool selectionChanged = false;
				pivot.SelectionChanged += delegate(object sender, SelectionChangedEventArgs e)
				{
					selectionChanged = true;
				};
				pivot.add_ManipulationCompleted(delegate(object sender, ManipulationCompletedEventArgs e)
				{
					if (!selectionChanged)
					{
						return;
					}
					selectionChanged = false;
					if (pivotIndex != pivot.SelectedIndex)
					{
						return;
					}
					bool fromRight = e.get_TotalManipulation().get_Translation().get_X() <= 0.0;
					List<FrameworkElement> list = Enumerable.ToList<FrameworkElement>(list.GetItemsInView());
					for (int i = 0; i < list.get_Count(); i++)
					{
						FrameworkElement frameworkElement_0 = list.get_Item(i);
						list.get_Dispatcher().BeginInvoke(delegate
						{
							IEnumerable<DependencyObject> enumerable = Enumerable.Where<DependencyObject>(frameworkElement_0.Descendants(), (DependencyObject dependencyObject_0) => MetroInMotion.GetAnimationLevel(dependencyObject_0) > -1);
							using (IEnumerator<DependencyObject> enumerator = enumerable.GetEnumerator())
							{
								while (enumerator.MoveNext())
								{
									FrameworkElement element = (FrameworkElement)enumerator.get_Current();
									MetroInMotion.GetSlideAnimation(element, fromRight).Begin();
								}
							}
						});
					}
				});
			});
		}
		public static void Peel(this IEnumerable<FrameworkElement> elements, Action endAction)
		{
			List<FrameworkElement> list = Enumerable.ToList<FrameworkElement>(elements);
			FrameworkElement frameworkElement = Enumerable.Last<FrameworkElement>(list);
			double num = 0.0;
			using (List<FrameworkElement>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FrameworkElement current = enumerator.get_Current();
					Storyboard peelAnimation = MetroInMotion.GetPeelAnimation(current, num);
					if (current.Equals(frameworkElement))
					{
						peelAnimation.add_Completed(delegate(object sender, EventArgs e)
						{
							endAction.Invoke();
						});
					}
					peelAnimation.Begin();
					num += 50.0;
				}
			}
		}
		public static IEnumerable<FrameworkElement> GetItemsInView(this ItemsControl itemsControl)
		{
			MetroInMotion.<GetItemsInView>d__12 <GetItemsInView>d__ = new MetroInMotion.<GetItemsInView>d__12(-2);
			<GetItemsInView>d__.<>3__itemsControl = itemsControl;
			return <GetItemsInView>d__;
		}
		private static Storyboard GetPeelAnimation(FrameworkElement element, double delay)
		{
			PlaneProjection planeProjection = new PlaneProjection();
			planeProjection.set_CenterOfRotationX(-0.1);
			PlaneProjection planeProjection2 = planeProjection;
			element.set_Projection(planeProjection2);
			double actualWidth = element.get_ActualWidth();
			double num = Math.Atan(1000.0 / (actualWidth / 2.0));
			num = num * 180.0 / 3.1415926535897931;
			Storyboard storyboard = new Storyboard();
			storyboard.set_BeginTime(new TimeSpan?(TimeSpan.FromMilliseconds(delay)));
			storyboard.get_Children().Add(MetroInMotion.CreateAnimation(0.0, -(180.0 - num), 0.3, "RotationY", planeProjection2));
			storyboard.get_Children().Add(MetroInMotion.CreateAnimation(0.0, 23.0, 0.3, "RotationZ", planeProjection2));
			storyboard.get_Children().Add(MetroInMotion.CreateAnimation(0.0, -23.0, 0.3, "GlobalOffsetZ", planeProjection2));
			return storyboard;
		}
		private static DoubleAnimation CreateAnimation(double from, double double_0, double duration, string targetProperty, DependencyObject target)
		{
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_To(new double?(double_0));
			doubleAnimation.set_From(new double?(from));
			doubleAnimation.set_EasingFunction(new SineEase());
			doubleAnimation.set_Duration(TimeSpan.FromSeconds(duration));
			Storyboard.SetTarget(doubleAnimation, target);
			Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(targetProperty, new object[0]));
			return doubleAnimation;
		}
		private static Storyboard GetSlideAnimation(FrameworkElement element, bool fromRight)
		{
			double num = (double)(fromRight ? 80 : -80);
			double num2 = (double)MetroInMotion.GetAnimationLevel(element) * 0.1 + 0.1;
			TranslateTransform translateTransform = new TranslateTransform();
			translateTransform.set_X(num);
			TranslateTransform translateTransform2 = translateTransform;
			element.set_RenderTransform(translateTransform2);
			Storyboard storyboard = new Storyboard();
			storyboard.set_BeginTime(new TimeSpan?(TimeSpan.FromSeconds(num2)));
			storyboard.get_Children().Add(MetroInMotion.CreateAnimation(num, 0.0, 0.8, "X", translateTransform2));
			return storyboard;
		}
	}
}
